﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using PeapodCatcher.Properties;
using Hardcodet.Wpf.TaskbarNotification;

namespace PeapodCatcher
{
    /// <summary>
    /// Interaction logic for Tray.xaml
    /// </summary>
    public partial class Tray : Window
    {
        private Options options;
        private bool optionsIsOpen = false;
        private System.Threading.Timer timer;
        private bool threadIsRunning = false;
        private bool isActive = false;
        //private bool menuVisible = false;
        //private System.Windows.Forms.NotifyIcon icon;
        private TaskbarIcon icon;
        //private System.Windows.Forms.ContextMenu menu;

        public Tray()
        {
            InitializeComponent();
            icon = new TaskbarIcon();
            icon.ContextMenu = this.Resources["menu"] as ContextMenu;
            icon.Visibility = Visibility.Visible;
            icon.ToolTip = "Peapod Catcher";
            icon.Icon = Properties.Resources.SysTray_Default;
            DataBaseHealthCheck.VerifyDatabase();
            options = new Options();

            isActive = true;


            Log.LogMessage("Service", "Register Callback");
            TimerCallback callback = new TimerCallback(TimerEvent);
            timer = new System.Threading.Timer(callback, null, 0, (Settings.Default.RefreshPeriod * 60000));
        }


        private void TimerEvent(object state)
        {
            Log.LogMessage("Service", "TimerEvent Has Fired");

            //IdleTime idle = new IdleTime();
            //Log.LogMessage("Service", "UserIdleTime: " + idle.GetLastUserInput());

            if (!isActive || threadIsRunning) return;
            threadIsRunning = true;
            try
            {
                // Only execute if the user has been idle for 5 minutes or more
                // Note: Does not currently work in Vista
                //if (idle.GetLastUserInput() >= (Properties.Settings.Default.IdleTime * 60))
                //{
                Updater updater = new Updater();
                Log.LogMessage("Service", "Refreshing RSS");
                updater.Refresh();
                //}
                //else
                //{
                //    Log.LogMessage("Service", "Idling");
                //}
            }
            catch (Exception ex)
            {
                Log.LogError("TimerEvent Exception", ex);
            }
            threadIsRunning = false;
            Log.LogMessage("Service", "TimerEvent Has Completed");
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            icon.Visibility = Visibility.Hidden;
            icon.Dispose();
            icon = null;

            Log.LogMessage("Service", "Service is stopping");
            isActive = false;
            timer.Dispose();
            //System.Windows.Application.Exit();
        }

        private void ExitController(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AboutBox(object sender, EventArgs e)
        {
            IdleTime idle = new IdleTime();
            MessageBox.Show("About TrayApp! IdleTime: " + idle.GetLastUserInput());
        }

        private void Pause(object sender, EventArgs e)
        {
            isActive = false;
            icon.Icon = Properties.Resources.Systray_Paused;
        }

        private void Resume(object sender, EventArgs e)
        {
            isActive = true;
            icon.Icon = Properties.Resources.Icon_Default;
        }

        private void Feeds(object sender, EventArgs e)
        {
            ShowOptionsForm(0);
        }

        private void Options(object sender, EventArgs e)
        {
            ShowOptionsForm(1);
        }

        private void ShowOptionsForm(int tabIndex)
        {
            if (optionsIsOpen) {
                options.Activate();
                options.BringIntoView();
            }
            else {
                if (options == null) options = new Options();
                options.Closed += new EventHandler(options_Closed);
                options.Show();
                optionsIsOpen = true;
            }
        }

        void options_Closed(object sender, EventArgs e)
        {
            optionsIsOpen = false;
            options = null;
        }


        private void Feeds_Click(object sender, RoutedEventArgs e)
        {
            ShowOptionsForm(0);
        }
        private void Options_Click(object sender, RoutedEventArgs e)
        {
            ShowOptionsForm(1);
        }
        private void About_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("About Peapod Catcher");
        }
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }



    }
}
